package io.flash.rpc.server;

import io.flash.rpc.protocol.PacketCodec;
import io.flash.rpc.protocol.RpcRequestPacket;
import io.flash.rpc.protocol.RpcRequestPacketHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyServer {
    public void start() {
        new Thread(() -> new ServerBootstrap().group(new NioEventLoopGroup(), new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();

                        pipeline.addLast(new PacketCodec());
                        pipeline.addLast(new RpcRequestPacketHandler());
                    }
                })
                .bind(8000)).start();
    }
}
